package com.wanmait.community.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanmait.community.entity.Unit;
import com.wanmait.community.mapper.BuildingMapper;
import com.wanmait.community.mapper.UnitMapper;
import com.wanmait.community.service.UnitService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class UnitServiceImpl extends ServiceImpl<UnitMapper, Unit> implements UnitService{
    @Resource
    private UnitMapper unitMapper;
    @Resource
    private BuildingMapper buildingMapper;
    @Override
    public Unit selectUnitNameAndBuildingIdByUnit(Unit unit) {
        return unitMapper.selectUnitByUnameAndBuildingId(unit);
    }

    @Override
    public List<Unit> findAll() {
        List<Unit> unitList = unitMapper.selectList(null);
        unitList.forEach(u -> {
            u.setBuilding(buildingMapper.selectById(u.getBuildingId()));
        });
        return unitList;
    }

    @Override
    public List<Unit> findUnitByBuildingId(Integer buildingId) {
        List<Unit> unitList = unitMapper.findUnitByBuildingId(buildingId);
        return unitList;
    }

    @Override
    public void deleteUnitByBuildingId(Integer buildingId) {
        unitMapper.deleteUnitByBuildingId(buildingId);
    }

    @Override
    public boolean save(Unit entity) {
        return super.save(entity);
    }
}
